<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none"
           v-if="!showAddress">
        您需要先添加收货地址才可提交订单。
      </div>
      <ul v-else>
        <li>
          <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
        </li>
        <li>
          <span>联系方式：</span>{{
            showAddress.contact.substr(0, 2) +
            '*******' +
            showAddress.contact.substr(-1, 2)
          }}
        </li>
        <li><span>收货地址：</span>{{ showAddress.fullLocation }}</li>
      </ul>
      <a href="javascript:;"
         @click="handleaddress(showAddress)">修改地址</a>
    </div>
    <div class="action">
      <XtxButton class="btn"
                 @click="openDialog()">切换地址</XtxButton>
      <XtxButton class="btn"
                 @click="handleaddress({})">添加地址</XtxButton>
    </div>
  </div>
  <!-- 对话框组件 -->
  <XtxDialog title="修改地址"
             v-model:visible="visible">
    <div class="text item"
         :class="{
        active:
          (selectedaddress ? selectedaddress.id : showAddress.id) === item.id
      }"
         v-for="item in list"
         :key="item.id"
         @click="selectedaddress = item">
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ item.receiver }}
        </li>
        <li><span>联系方式：</span>{{ item.contact }}</li>
        <li>
          <span>收货地址：</span>{{ item.fullLocation.replace(/ /g, '') + item.address }}
        </li>
      </ul>
    </div>
    <template #footer>
      <XtxButton type="gray"
                 style="margin-right: 20px"
                 @click="closeDialog()">取消</XtxButton>
      <XtxButton type="primary"
                 @click="changeselected()">确认</XtxButton>
    </template>
  </XtxDialog>
  <AddressEdit ref="AddressEditcom"
               @addsuccess="addsuccess" />
</template>
<script>
import { ref } from 'vue'
import AddressEdit from './address-edit.vue'
export default {
  props: {
    list: {
      type: () => [],
      default: []
    }
  },
  name: 'CheckoutAddress',
  // 如果是在代码片段里面触发emit，vue3强制声明
  emits: ['change'],
  components: { AddressEdit },
  setup (props, { emit }) {
    const showAddress = ref(null)
    const one = props.list.find((item) => item.isDefault === 0)
    if (one) {
      // 有默认地址
      showAddress.value = one
    } else {
      // 没有默认地址
      showAddress.value = props.list.length && props.list[0]
    }
    // 将默认地址id传递给父组件
    if (showAddress.value) {
      emit('change', showAddress.value.id)
    }
    // 控制对话框的显示和隐藏
    const visible = ref(false)
    const openDialog = () => {
      visible.value = true
    }
    const closeDialog = () => {
      console.log(visible.value)
      visible.value = false
    }

    // 对话框中选择的地址
    const selectedaddress = ref(null)
    // 确认选中
    const changeselected = () => {
      showAddress.value = selectedaddress.value
      // 将地址id传给父组件
      emit('change', selectedaddress.value?.id)
      visible.value = false
    }

    // 添加或者修改地址对话框打开
    const AddressEditcom = ref(null)
    const handleaddress = (obj) => {
      AddressEditcom.value.open(obj)
    }

    // 添加一个地址
    const addsuccess = (form) => {
      // 先看是否地址已经存在，若不存在则是添加，若存在则是修改
      const obj = props.list.find((item) => item.id === form.id)
      if (obj) {
        // 修改地址
        for (const key in form) {
          obj[key] = form[key]
        }
      } else {
        // 添加地址
        // 这里直接通过地址修改list(复杂对象可以这样修改)
        // 要通过浅克隆的方式
        const newform = JSON.parse(JSON.stringify(form))
        // eslint-disable-next-line vue/no-mutating-props
        props.list.unshift(newform)
      }
    }
    return {
      showAddress,
      visible,
      openDialog,
      closeDialog,
      selectedaddress,
      changeselected,
      AddressEditcom,
      handleaddress,
      addsuccess
    }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
